<?php
set_time_limit(0); //防止执行超时
ob_end_clean();   //清空并关闭输出缓冲区

if(!isset($_GET["id"])){
    echo "pls input id!";
    return;
}

include("base_function.php");
include("base/db.php");

function analyse(&$html)
{
    //仓储所在地，用户ID，用户url，销量、标题、分类、页面详细地址、价格
    //用户ID
    //用户地址
    $reg = '|<td align="left" valign="middle" class="contentValueFont">(.*?)</td>|';
    preg_match_all ($reg,$html,$out, PREG_PATTERN_ORDER);
    if(count($out) != 0){
        $first = true;
        $lies = "catalog_id,time";
        $insert_sql = "insert into pm_sale_time ($lies) values ";
        for($i = 0;$i < count($out[1]);++$i){
            if($first){
                $vals = "";
                $first = false;
            }else{
                $vals = ",";
            }
            $vals .= "('".$_GET["id"]."','".$out[1][$i]."')\r\n";
            $insert_sql .= $vals;
        }
        exec_db( $insert_sql);
    }
    
}
function doit()
{
    open_db();
    exec_db( "delete from pm_sale_time where catalog_id=".$_GET["id"]);
    $sold_urls = query_db("select sold_url from pm_good_info where sold_url <> '0' and id in (select id from pm_product where catalog_id=".$_GET["id"].") order by cast(sold as unsigned) desc limit 0,300");
    foreach ($sold_urls as $sold_url){
        //echo $sold_url["url"]."\r\n";
        $html = http_response($sold_url["sold_url"]);
        $html = htmlspecialchars_decode($html,ENT_QUOTES);
        //file_put_contents("sold.txt",$html);
        analyse($html);
    }
    close_db();
    return;
}

doit();
echo "ok";

?>
